// pages/confirmOrder/confirmOrder.js
let app = getApp();
import util from '../../../utils/util.js';
import WxParse from '../../../libs/wxParse/wxParse.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    remark:'',
    type:'',
    id:'',
    pid:'',
    adressHas:false,
    addressList:[],
    confirmP:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type
    let id = options.id
    let pid = options.pid
    console.log(type,id,pid)
    this.setData({
      type,id,pid
    })
    this.getPinInfo()
  },
// 地址列表    ecapi.consignee.list
  getAddressList() {
    let self = this
    util.request(util.apiUrl + 'ecapi.consignee.list', 'POST').then(res => {
      //定义 地址列表， 地址有无判断， 默认地址
      let addressList = res.consignees
      let adressHas = false
      self.setData({addressList})
      if (self.data.addressList.length == 0){
          self.setData({adressHas:adressHas})
      }else{
          adressHas = true
          self.setData({adressHas:adressHas})
      }
    }).catch(err => {
      util.notLogin(err);
    });
  },
// 生命周期函数--监听页面显示
  onShow: function () { 
    this.getAddressList();
  },
//得到产品信息
  getPinInfo(){
    let self = this
    wx.request({
      url: util.apiUrl + 'xcx.pin.product', 
      data: {
         product:self.data.id
      },
      header: {
          'content-type': 'application/json' // 默认值
      },
      method:'POST',
      success: function(res) {
        console.log(res)
        self.setData({
          'product':res.data.product
        })
      }
    })
  },

//获取备注 存储用户备注
  get_remark(event){
    // 存储用户备注信息
    let remark = event.detail.value
    console.log(remark)
    this.setData({
      remark
    })
  },
//立刻支付
  confirm(){

    if(!this.data.adressHas){
      wx.showToast({
        title: '亲，您还没填写地址!',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    let self = this
    let remark = self.data.remark//得到数据中的用户备注信息。
    let addressList = self.data.addressList //地址列表  
    let addressTrue = {}
    let adressHas = self.data.adressHas

    let confirmP = self.data.confirmP
    if (confirmP){
      self.setData({
        'confirmP':!self.data.confirmP
      })
      //得到默认地址
      for (let i = 0; i < addressList.length; i++) {
        if(addressList[i].is_default == true){
          addressTrue = addressList[i]
        }
      }
      if (adressHas){
        let cacheOpenid = wx.getStorageSync('openid');  //获取缓存Openid
        let typePin = 2  //type： 3 三人拼 ：  人人拼
        if (self.data.type != 'people'){
          typePin = 3
        }

        let data = {
          "shop":1,
          "consignee": addressTrue.id,
          "shipping":4,  
          "comment": self.data.remark,
          "product": self.data.id,    
          "amount":1,
          "openid":cacheOpenid,
          "pid": self.data.pid,
          "type": typePin
        } 
        //调用支付借口
        util.request(util.apiUrl + 'xcx.pin.x_purchase', 'POST' ,data).then(res => {
          //定义 地址列表， 地址有无判断， 默认地址
          wx.setStorageSync('order', res.order)//订单所有信息
          wx.setStorageSync('sn', res.order.sn)//订单号

          wx.requestPayment({
            'timeStamp': res.wxpay.timestamp,
            'nonceStr': res.wxpay.nonce_str,
            'package': res.wxpay.packages,
            'signType': 'MD5',
            'paySign': res.wxpay.sign,
            'success':function(res){
              console.log(res);
              let order = wx.getStorageSync('order')//获取支付订单  详情
              let type = self.data.type
              let id = self.data.id
              if (type != 'people'){
                let pid = order.pid //获取订单中的 pid
                wx.navigateTo({
                  url: '../paymentSuccess/paymentSuccess?type=' + type + '&id=' + id + '&pid=' + pid,
                })
              }else{
                let pid = 0 //乱写的pid
                wx.navigateTo({
                  url: '../paymentSuccess/paymentSuccess?type=' + type + '&id=' + id + '&pid=' + pid,
                })
              }
              self.setData({
                'confirmP': !self.data.confirmP
              })
            },
            'fail':function(res){
              console.log(res);
              let sn = wx.getStorageSync('sn')//订单号
              let type = self.data.type
              let id = self.data.id
              let pid = self.data.pid
              wx.navigateTo({
                url: '../paymentFail/paymentFail?sn=' + sn + '&type=' + type + '&id=' + id +'&pid=' + pid,
              })
              self.setData({
                'confirmP': !self.data.confirmP
              })
            }
          })
        }).catch(err => {
          console.log(err)
        });
      }else{
        wx.showToast({
          title: '请添加地址！',
          icon: 'none',
          duration: 2000
        })
        self.setData({
          'confirmP': !self.data.confirmP
        })
        return
      }
    }
  }
})